﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;

/*******************************************************************************************
                           LICENSE INFORMATION
*****************************************************************************************
   System_IO Version 1.0.0.0
 
   System.IO File utulities for operations working with the System.IO Namespace

   Copyright (C) 2007  
   Richard L. McCutchen 
   Email: richard@psychocoder.net
   Created: 01SEP07

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************************/

namespace Flux3GUI
{
    class FileControl
    {

        private RichTextBox _exceptionOutputWindow;

        public FileControl()
        {
            _exceptionOutputWindow = null;
        }

        #region "Exception Output Control"
        /// <summary>
        /// Methods to get and set the exception message output method of the file controller
        /// </summary>
        public RichTextBox exceptionOutputWindow
        {
            get{ return _exceptionOutputWindow; }
            set { _exceptionOutputWindow = value; }
        }

        /// <summary>
        /// Method to display the passed in exception to the exceptionOutputWindow
        /// </summary>
        /// <param name="exception">String Containing the Exception</param>
        private void displayException(String exception)
        {
            _exceptionOutputWindow.Invoke(new EventHandler(delegate
            {
                _exceptionOutputWindow.SelectedText = string.Empty;
                //_exceptionOutputWindow.SelectionFont = new Font(_exceptionOutputWindow.SelectionFont, FontStyle.Bold);
                _exceptionOutputWindow.AppendText(exception);
                _exceptionOutputWindow.AppendText("\n");
                _exceptionOutputWindow.ScrollToCaret();
            }));
        }
        #endregion

        #region "Create Files"
        /// <summary>
        /// Method to create a file
        /// </summary>
        public void createFile(string fileName)
        {
            try
            {
                System.IO.File.Create(fileName);
            }
            catch (Exception ex)
            {
                displayException(ex.Message);
            }
        }
        #endregion

        #region "Read & Write files"
        /// <summary>
        /// Method for writing to a file
        /// </summary>
        /// <param name="file">File to write to</param>
        /// <param name="msg">Text to write to the file</param>
        public void WriteToFile(string file, string msg)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //create a TextWriter then open the file
                using (TextWriter writer = new StreamWriter(file))
                {
                    //now write the message to the file
                    writer.WriteLine(msg);
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
            }

        }

        /// <summary>
        /// Method for writing text to a specified line
        /// </summary>
        /// <param name="file">File to write to</param>
        /// <param name="line">Line to insert text</param>
        /// <param name="msg">Text to insert</param>
        public void WriteToSpecifiedLine(string file, int line, string msg)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //make sure the file actually exists
                //if not create and open it
                if (!System.IO.File.Exists(file))
                {
                    displayException("File does not exist... creating");
                    System.IO.File.Create(file);
                }
                //open the file
                using (StreamReader reader = new StreamReader(file))
                {
                    //get the contents of the file
                    string lineToInsert = reader.ReadToEnd();
                    //insert the specified line
                    lineToInsert.Insert(line, msg);
                    //now we need to rewrite the text in the file
                    using (StreamWriter writer = new StreamWriter(file, false))
                    {
                        //write the file with the new line
                        writer.Write(lineToInsert);
                    }
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for writing to the beginning of a file
        /// </summary>
        /// <param name="file">File to write to</param>
        /// <param name="msg">Text to insert</param>
        public void WriteToBeginningOfFile(String file, String msg)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //make sure the file actually exists
                //if not create and open it
                if (!System.IO.File.Exists(file))
                {
                    displayException("File does not exist... creating");
                    System.IO.File.Create(file);
                }
                //open the file
                using (StreamReader reader = new StreamReader(file))
                {
                    //get the contents of the file
                    string currentContents = reader.ReadToEnd();
                    //insert at the beginning of the current contents
                    currentContents.Insert(0, msg);
                    //now we need to rewrite the text in the file
                    using (StreamWriter writer = new StreamWriter(file, false))
                    {
                        //write the file with the new line
                        writer.Write(currentContents);
                    }
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for reading a single line in a file
        /// </summary>
        /// <param name="file">File name to read from</param>
        /// <returns>The text in that line</returns>
        public string readALine(string file)
        {
            //create a variable to hold our line
            string lineContents = string.Empty;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //create a new TextReader then open the file
                using (TextReader reader = new StreamReader(file))
                {
                    //read a single line from the file
                    lineContents = reader.ReadLine();
                    return lineContents;
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
                return lineContents;
            }

        }

        /// <summary>
        /// Method to read a specified line in a text file
        /// </summary>
        /// <param name="file">File to read from</param>
        /// <param name="line">Line number to read</param>
        /// <returns></returns>
        public string ReadSpecifiedLine(string file, int line)
        {
            //create a variable to hold the contents of the file
            string fileContents = string.Empty;
            //create a variable to hold our line contents
            string lineContent = string.Empty;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                using (StreamReader stream = new StreamReader(file))
                {
                    fileContents = stream.ReadToEnd().Replace("\r\n", "\n").Replace("\n\r", "\n");
                    string[] linesArray = fileContents.Split(new char[] { '\n' });
                    lineContent = linesArray[line];
                    return lineContent;
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
                return lineContent;
            }
        }

        /// <summary>
        /// Method for reading a text file a line at a time
        /// and adding it to a string to return to calling method
        /// </summary>
        /// <param name="file">File to read</param>
        /// <returns>File contents in a string</returns>
        public string ReadFileLineAtATime(string file)
        {
            //create a string variable to hold the file contents
            string fileContents = string.Empty;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //create a new TextReader then open the file
                using (TextReader reader = new StreamReader(file))
                {
                    //loop through the entire file
                    while (reader.Peek() != -1)
                    {
                        //add each line to the fileContents variable
                        fileContents += reader.ReadLine().ToString();
                    }
                    //return the results
                    return fileContents;
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
                return fileContents;
            }
        }

        /// <summary>
        /// Method for reading an entire file at once
        /// </summary>
        /// <param name="file">File to read</param>
        /// <returns>File contents in a string</returns>
        public string ReadEntireFile(string file)
        {
            //create a string variable to hold the contents of the file
            string fileContents = string.Empty;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //create a new TextReader and open our file
                using (TextReader reader = new StreamReader(file))
                {
                    //now read the entire file at once into our variable
                    fileContents = reader.ReadToEnd().ToString();
                }
                return fileContents;
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
                return fileContents;
            }
        }

        /// <summary>
        /// Method for reading a file if it exists
        /// </summary>
        /// <param name="file">File to read</param>
        /// <returns>File contents in a string</returns>
        public string ReadEntireFileIfExists(string file)
        {
            //create a string variable to hold the contents of the file
            string fileContents = string.Empty;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check to see if the file exists
                if (System.IO.File.Exists(file))
                {
                    //create a new TextReader and open our file
                    using (TextReader reader = new StreamReader(file))
                    {
                        //now read the entire file at once into our variable
                        fileContents = reader.ReadToEnd();
                    }
                    return fileContents;
                }
                else
                {
                    throw new FileNotFoundException(file + " could not be found");
                }
            }
            catch (Exception ex)
            {
                //handle your errors here
                displayException(ex.Message);
                return fileContents;
            }
        }
        #endregion

        #region "File Conversions"
        /// <summary>
        /// Function to convert a delimited file to an XML document
        /// </summary>
        /// <param name="delimiter">Delimiter to check for in the file</param>
        /// <param name="file">File to convert</param>
        /// <param name="xmlFileName">Name of the resulting XML document</param>
        public void ConvertDelimitedToXML(char delimiter, string file, string xmlFileName)
        {
            //create the objects we need
            System.Data.DataSet xmlDataSet = new System.Data.DataSet();
            System.Data.DataTable xmlTable = new System.Data.DataTable();
            System.Data.DataRow xmlRows;
            //always use a try...catch block to catch any errors
            try
            {
                using (StreamReader reader = new StreamReader(file))
                {
                    //set the DataSetName of the DataSet
                    xmlDataSet.DataSetName = "YourName";
                    //set the NameSpace of the DataSet
                    xmlDataSet.Namespace = "YourNamespace";

                    //make sure we're at the beginning of the file
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    //add the header columns
                    foreach (string fields in reader.ReadLine().Split(delimiter))
                    {
                        //xmlDataSet.Tables(0).Columns.Add(fields);
                        xmlTable.Columns.Add(fields);
                    }
                    //now add the rows
                    while (reader.Peek() != -1)
                    {
                        xmlRows = xmlTable.NewRow();
                        foreach (string fields in reader.ReadLine().Split(delimiter))
                        {
                            xmlTable.Rows.Add(fields);
                        }
                        //add the new rows to the table
                        xmlTable.Rows.Add(xmlRows);
                    }
                    //add the table to the DataSet
                    xmlDataSet.Tables.Add(xmlTable);
                    //write out the XML
                    xmlDataSet.WriteXml(xmlFileName);
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for converting an XML Document to a comma delimited file
        /// </summary>
        /// <param name="xmlDoc">XML document to convert</param>
        /// <param name="fileName">Name of the resulting file</param>
        public void ConvertXMLDocToDelimitedFile(string xmlDoc, string fileName)
        {
            //create the objects we need
            System.Data.DataSet set = new System.Data.DataSet();
            //use a FileStream to open the xml document provided
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                using (FileStream stream = new FileStream(xmlDoc, FileMode.Open))
                {
                    //now will the dataset with the data from the xml document
                    set.ReadXml(stream);
                    //call the ConvertDataSet To DelimitedFile method
                    //to convert the DataSet to a comma delimited file
                    ConvertDataSetToDelimitedFile(set, fileName);
                }
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for converting a DataSet to
        /// a comma delimited file
        /// </summary>
        /// <param name="set">DataSet to convert</param>
        /// <param name="fileName">Name of the file to create</param>
        public void ConvertDataSetToDelimitedFile(System.Data.DataSet set, string fileName)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //create the object we need
                System.Data.DataTable table = new System.Data.DataTable();
                System.Text.StringBuilder header = new System.Text.StringBuilder();
                System.Text.StringBuilder body = new System.Text.StringBuilder();
                //get the datatable from the dataset
                table = set.Tables[0];
                //generate the column names
                foreach (System.Data.DataColumn column in table.Columns)
                {
                    header.Append(",");
                    header.Append(column.ColumnName);
                }
                if (header.Length > 0) header.Remove(0, 1).Append("\r\n");
                //write the rows
                foreach (System.Data.DataRow row in table.Rows)
                {
                    foreach (object columns in row.ItemArray)
                    {
                        body.Append(columns.ToString());
                        body.Append(",");
                    }
                    if (body.Length > 0) body.Remove(body.Length - 1, 1).Append("\r\n");
                }
                //combine the 2 items into a single string
                header.Append(body.ToString());
                //write the string to a file
                using (StreamWriter writer = new StreamWriter(fileName))
                {
                    writer.Write(body.ToString());
                }
                //delete the objects to free up memory
                header = null;
                body = null;
            }
            catch (Exception ex)
            {
                //deal with any errors
                displayException(ex.Message);
            }
        }
        #endregion

        #region "Move Files"
        /// <summary>
        /// Procedure for moving a single file
        /// </summary>
        /// <param name="file">Name of the file to move</param>
        /// <param name="newPath">Destination for the file</param>
        public void MoveSingleFile(string file, string newPath)
        {
            //use a try...catch block to deal with any exceptions that occur
            //or that we create if needed
            try
            {
                //first make sure the file exists
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " cannot be found");
                }
                else
                {
                    //make sure the new directory exists
                    //if it doesnt then create it
                    if (!Directory.Exists(newPath))
                    {
                        Directory.CreateDirectory(newPath);
                    }
                    //now that the directory is taken care of we need to move the file
                    System.IO.File.Move(file, newPath);
                    displayException("The file " + file + " moved successfully!");
                }
            }
            catch (Exception ex)
            {
                //handle errors here
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for moving all the files in
        /// a specified directory
        /// </summary>
        /// <param name="dir">Origination directory for the files</param>
        /// <param name="destDir">Destination directory for the files</param>
        public void MoveAllFilesInDirectory(string dir, string destDir)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //first we need to make sure the directory exists
                //otherwise an exception will be thrown
                if (!System.IO.Directory.Exists(dir))
                {
                    //throw an exception to be used later
                    throw new DirectoryNotFoundException(dir + " doesnt exist! Please try again.");
                }
                else
                {
                    //get the information about the directory provided
                    DirectoryInfo dirInfo = new DirectoryInfo(dir);
                    //put all the files into a string array
                    FileInfo[] files = dirInfo.GetFiles("*.*");
                    //now loop through the list moving the files
                    foreach (FileInfo file in files)
                    {
                        file.MoveTo(destDir);
                    }
                    //let the user know
                    displayException("All files moved successfully!");
                }
            }
            catch (Exception ex)
            {
                //handle any errors
                displayException(ex.Message);
            }
        }
        #endregion

        #region "Copy files"
        /// <summary>
        /// Method for copying a single file
        /// </summary>
        /// <param name="origFile">Path & file of the file to copy</param>
        /// <param name="destFile">Path & name of the destination file</param>
        public void CopySingleFile(string origFile, string destFile)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the destination file exists,
                //if it does we need to delete it, .Copy
                //will raise an exception otherwise
                if (System.IO.File.Exists(destFile))
                {
                    System.IO.File.Delete(destFile);
                }
                //now we can copy the file
                System.IO.File.Copy(origFile, destFile);
                //now delete the original file
                //TODO: Comment this line if you dont
                //want to delete the original file
                System.IO.File.Delete(origFile);
                displayException("File copied successfully");
            }
            catch (Exception ex)
            {
                //handle any errors that occurred
                Console.Write(ex.Message);
            }
        }

        /// <summary>
        /// Method for copying all the files
        /// in a specified directory
        /// </summary>
        /// <param name="origDir">Directpry the files are in</param>
        /// <param name="destDir">Directory the files are being copied to</param>
        public void CopyAllFilesInDirectory(string origDir, string destDir)
        {
            //get all the info about the original directory
            DirectoryInfo dirInfo = new DirectoryInfo(origDir);
            //retrieve all the files in the original directory
            FileInfo[] files = dirInfo.GetFiles(origDir);
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //loop through all the files and copy them
                foreach (string file in System.IO.Directory.GetFiles(origDir))
                {
                    FileInfo origFile = new FileInfo(file);
                    FileInfo destFile = new System.IO.FileInfo(file.Replace(origDir, destDir));
                    //copy the file, ose the OverWrite overload to overwrite
                    //destination file if it exists
                    System.IO.File.Copy(origFile.FullName, destFile.FullName, true);
                    //TODO: If you dont want to remove the original
                    //files comment this line out
                    System.IO.File.Delete(origFile.FullName);
                }
                displayException("All files in " + origDir + " copied successfully!");
            }
            catch (Exception ex)
            {
                //handle any errors that may have occurred
                displayException(ex.Message);
            }
        }
        #endregion

        #region "Delete Files"
        /// <summary>
        /// Method for deleting a single file
        /// </summary>
        /// <param name="file">The file to delete</param>
        public void DeleteSingleFile(string file)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //make sure the file exists
                //if it doesnt raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " cannot be found!");
                }
                else
                {
                    //delete the file
                    System.IO.File.Delete(file);
                    displayException(file + " deleted successfully!");
                }
            }
            catch (Exception ex)
            {
                //handle errors that may have occurred
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for deleting all the files
        /// in a specified directory
        /// </summary>
        /// <param name="dir">Directory to delete from</param>
        public void DeleteAllFilesInDirectory(string dir)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //first make sure the directory exists
                //if it doesnt and we try to delete the 
                //files an exception is thrown
                if (!System.IO.Directory.Exists(dir))
                {
                    //throw the exception to be
                    //dealt with later
                    throw new DirectoryNotFoundException(dir + " cannot be found! Please retry your request");
                }
                else
                {
                    //retrieve all the files and put them into a string array
                    string[] files = Directory.GetFiles(dir);
                    //now loop through all the files and delete them
                    foreach (string file in files)
                    {
                        System.IO.File.Delete(file);
                    }
                    //let the user know it was successful
                    displayException("All files deleted successfully!");
                }
            }
            catch (Exception ex)
            {
                //handle any errors that occurred
                displayException(ex.Message);
            }
        }
        #endregion

        #region "File Information/Properties"
        /// <summary>
        /// Method to set the ReadOnly property of a file
        /// </summary>
        /// <param name="file">File to set property of</param>
        /// <param name="readOnly">Value of ReadOnly</param>
        public void SetReadOnly(string file, bool readOnly)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the file exists, if it 
                //doesnt exist raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " could not be found!");
                }
                else
                {
                    //set the readonly status to the parameter
                    //passed to the method
                    FileInfo fInfo = new FileInfo(file);
                    fInfo.IsReadOnly = readOnly;
                }


            }
            catch (Exception ex)
            {
                //handle any errors that occurred
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for setting the LastAccessTime of a file
        /// </summary>
        /// <param name="file">File to set property for</param>
        public void SetLastAccessTime(string file)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the file exists, if it 
                //doesnt exist raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " could not be found!");
                }
                else
                {
                    //set the last access date of the file
                    FileInfo fInfo = new FileInfo(file);
                    DateTime access = DateTime.Now;
                    fInfo.LastAccessTime = access;
                }
            }
            catch (Exception ex)
            {
                //handle any errors that may have occurred
                displayException(ex.Message);
            }
        }

        /// <summary>
        /// Method for returning the ReadOnly status of a file
        /// </summary>
        /// <param name="file">File to retrieve status from</param>
        /// <returns>Boolean value of the ReadOnly property</returns>
        public Boolean ReturnReadOnlyStatus(string file)
        {
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the file exists, if it 
                //doesnt exist raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " could not be found!");
                }
                else
                {
                    Boolean readOnly = new FileInfo(file).IsReadOnly;
                    return readOnly;
                }
            }
            catch (Exception ex)
            {
                //handle any errors that occurred
                displayException(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Method to retrieve the LastAccessTime of a file
        /// </summary>
        /// <param name="file">File to retrieve info from</param>
        /// <returns>DateTime value of the LastAccessTime property</returns>
        public DateTime ReturnLastAccessProperty(string file)
        {
            DateTime lastAccess;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the file exists, if it 
                //doesnt exist raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " could not be found!");
                }
                else
                {
                    lastAccess = new FileInfo(file).LastAccessTime;
                    return lastAccess;
                }
            }
            catch (Exception ex)
            {
                //handle any errors that occurred
                displayException(ex.Message);
                lastAccess = DateTime.Now;
                return lastAccess;
            }
        }

        /// <summary>
        /// Method for returning the file size
        /// </summary>
        /// <param name="file">File to retrieve size of</param>
        /// <param name="conversionType">What type of conversion do we want
        /// KB, MB, GB or Bytes</param>
        /// <returns>Size of the file in the specified conversion type</returns>
        public double ReturnFileSize(string file, string conversionType)
        {
            double fileSize = 0;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the file exists, if it 
                //doesnt exist raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " could not be found!");
                }
                else
                {
                    fileSize = new FileInfo(file).Length;
                    return ConvertSize(fileSize, conversionType);
                }
            }
            catch (Exception ex)
            {
                //handle any errors that occurred
                displayException(ex.Message);
                return fileSize;
            }
        }

        /// <summary>
        /// Method to determine if a file is open
        /// </summary>
        /// <param name="file">File to check</param>
        /// <returns>Boolean value</returns>
        public Boolean ReturnIsFileOpen(string file)
        {
            Boolean isOpen = false;
            //always use a try...catch to deal 
            //with any exceptions that may occur
            try
            {
                //check if the file exists, if it 
                //doesnt exist raise an error
                if (!System.IO.File.Exists(file))
                {
                    throw new FileNotFoundException(file + " could not be found!");
                }
                else
                {
                    FileStream stream = System.IO.File.OpenRead(file);
                    stream.Close();
                }
            }
            catch
            {
                isOpen = true;
            }
            return isOpen;
        }
        #endregion

        #region "Helpers"
        /// <summary>
        /// Function to convert the given bytes to either Kilobyte, Megabyte, or Gigabyte
        /// </summary>
        /// <param name="bytes">Double -> Total bytes to be converted</param>
        /// <param name="type">String -> Type of conversion to perform</param>
        /// <returns>Int32 -> Converted bytes</returns>
        /// <remarks></remarks>
        public double ConvertSize(double size, string type)
        {
            try
            {
                const int CONVERSION_VALUE = 1024;
                //determine what conversion they want
                switch (type)
                {
                    case "BY":
                        //convert to bytes (default)
                        return size;
                    case "KB":
                        //convert to kilobytes
                        return (size / CONVERSION_VALUE);
                    case "MB":
                        //convert to megabytes
                        return (size / squareValue(CONVERSION_VALUE));
                    case "GB":
                        //convert to gigabytes
                        return (size / cubeValue(CONVERSION_VALUE));
                    default:
                        //default
                        return size;
                }
            }
            catch (Exception ex)
            {
                displayException(ex.Message);
                return 0;
            }
        }

        /// <summary>
        /// Function to calculate the square of the provided number
        /// </summary>
        /// <param name="number">double -> Number to be squared</param>
        /// <returns>Double -> THe provided number squared</returns>
        /// <remarks></remarks>
        public double squareValue(double number)
        {
            return Math.Pow(number, 2);
        }

        /// <summary>
        /// Function to calculate the cube of the provided number
        /// </summary>
        /// <param name="number">double -> Number to be cubed</param>
        /// <returns>Double -> THe provided number cubed</returns>
        /// <remarks></remarks>
        public static double cubeValue(double number)
        {
            return Math.Pow(number, 3);
        }
        #endregion
    }
}
